package game.resources;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;

public class InputManager {
	
	private static InputManager inputManager = null;
	
	public final static int UP 			= 0;
	public final static int DOWN 		= 1;
	public final static int LEFT 		= 2;
	public final static int RIGHT 		= 3;
	public final static int BUTTON1 	= 4;
	//public final static int BUTTON2 	= 5;
	
	private GameContainer gc;
	private boolean[] keyPressedStates;
	private boolean[] keyHeldStates;
	
	private InputManager() {
		keyPressedStates = new boolean[6];
		keyHeldStates = new boolean[6];
	}
	
	public static InputManager getInstance() {
		if (inputManager == null) 
			inputManager = new InputManager();
		return inputManager;
	}
	
	public void setGameContainer(GameContainer gc) {
		this.gc = gc;
	}
	
	public void update() {
		keyPressedStates[UP] = gc.getInput().isKeyPressed(Input.KEY_UP);
		keyHeldStates[UP]    = gc.getInput().isKeyDown(Input.KEY_UP);
		
		keyPressedStates[DOWN] = gc.getInput().isKeyPressed(Input.KEY_DOWN);
		keyHeldStates[DOWN]    = gc.getInput().isKeyDown(Input.KEY_DOWN);
		
		keyPressedStates[LEFT] = gc.getInput().isKeyPressed(Input.KEY_LEFT);
		keyHeldStates[LEFT]    = gc.getInput().isKeyDown(Input.KEY_LEFT);
		
		keyPressedStates[RIGHT]  = gc.getInput().isKeyPressed(Input.KEY_RIGHT);
		keyHeldStates[RIGHT]     = gc.getInput().isKeyDown(Input.KEY_RIGHT);
		
		keyPressedStates[BUTTON1]  = gc.getInput().isKeyPressed(Input.KEY_SPACE);
		keyHeldStates[BUTTON1]     = gc.getInput().isKeyDown(Input.KEY_SPACE);
	}
	
	public boolean isKeyPressed(int key) 	{ return keyPressedStates[key]; }
	public boolean isKeyHeld(int key) 		{ return keyHeldStates[key]; }
}
